package com.infore.gateway.main.model;


import com.infore.gateway.main.enums.ResultStatus;
import com.infore.gateway.main.enums.ServiceReturnEnum;

import java.util.HashMap;
import java.util.Map;
/**
 * @ClassName: ResultEntity
 * @Description: 结果封装实体类
 * @author 谭红波
 * @date 2017年9月2日 timestamp
 *
 */
public class ResultEntity {
        int code; // 结果代码
        String message; // 消息提示
        Object content; // 具体结果内容

        public ResultEntity() {

        }

        public ResultEntity(ResultStatus resultStatus, String message, Object content) {
                this.code = resultStatus.getStatusCode();
                this.message = message;
                this.content = content;
        }

        public ResultEntity(String code, String msg) {
                this.code = Integer.parseInt(code);
                this.message = msg;
        }

        public ResultEntity(int code, String msg) {
                this.code = code;
                this.message = msg;
        }

        public ResultEntity(ResultStatus resultStatus, String message) {
                this(resultStatus, message, null);
        }

        public ResultEntity setMapValue(String key, Object value) {
                if (this.content == null) {
                        this.content = new HashMap<String, Object>();
                }
                assert content instanceof Map;
                ((Map<String, Object>) this.content).put(key, value);
                return this;
        }

        public ResultEntity setInfo(Object info) {
                return setMapValue("info", info);
        }

        public ResultEntity setDataList(Object list) {
                return setMapValue("dataList", list);
        }

        public ResultEntity setTotalRecords(Integer total) {
            return setMapValue("total", total);
        }



        public int getCode() {
                return code;
        }

        public void setCode(int code) {
                this.code = code;
        }

        public String getMessage() {
                return message;
        }

        public void setMessage(String message) {
                this.message = message;
        }

        public Object getContent() {
                return content;
        }

        public void setContent(Object content) {
                this.content = content;
        }


        public static ResultEntity retSuccessRes(Object t, String message) {
                ResultEntity res = new ResultEntity(ServiceReturnEnum.SUCCESS.getCode(), message);
                if (t != null) {
                        res.setContent(t);
                }
                return res;
        }

        public static ResultEntity retSuccessRes() {
                return new ResultEntity(ServiceReturnEnum.SUCCESS.getCode(), ServiceReturnEnum.SUCCESS.getMsg());
        }

        public static ResultEntity retDefException() {
                return new ResultEntity(ServiceReturnEnum.ERROR.getCode(), ServiceReturnEnum.ERROR.getMsg());
        }

        public static ResultEntity retDefException(String msg) {
                return new ResultEntity(ServiceReturnEnum.ERROR.getCode(), msg);
        }

        @Override
        public String toString() {
                return "ResultEntity [code=" + code + ", message=" + message + ", content=" + content + "]";
        }

}
